class Solution {
    private Map<Character, Character> map;
    public boolean isIsomorphic(String s, String t) {
        if (s.length() == 0) return true;
        map = new HashMap<>(Math.min(26, s.length()));
        Set<Character> used = new HashSet<>(Math.min(26, t.length()));
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                if (t.charAt(i) != map.get(s.charAt(i))) {
                    return false;
                }
            } else {
                if (used.contains(t.charAt(i))) {
                    return false;
                }
                map.put(s.charAt(i), t.charAt(i));
                used.add(t.charAt(i));
            }
        }
        return true;
    }
}
